package library.view;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import library.dao.Impl.BookTypeDaoImpl;
import library.pojo.Book;
import library.service.LibraryService;

public class AddBookDialog extends JDialog {
	private Book book;
	private JTextField isbnTextField;
	private JTextField bookNameTextField;
	private JTextField authorTextField;
	private JTextField dateTextField;
	private JComboBox bookStateComboBox;
	private JComboBox bookTypeComboBox;

	public AddBookDialog(String title, Book book) {
		this.book = book;
		this.setTitle(title);
		this.add(getBookPanel());
		this.setModal(true);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.validate();
	}

	private JPanel getBookPanel() {
		ImageIcon icon = new ImageIcon(MainJTPane.class.getResource("/title.png"));
		this.setIconImage(icon.getImage());
		JPanel bookPanel = new JPanel();
		isbnTextField = new JTextField(15);
		bookNameTextField = new JTextField(15);
		authorTextField = new JTextField(15);
		dateTextField = new JTextField(15);
		String[] ways = { "在馆", "借出" };
		bookStateComboBox = new JComboBox(ways);
		bookStateComboBox.setPreferredSize(new Dimension(170, 22));
		String[] way = { "马克思主义", "艺术", "历史、地理", "自然科学总论", "数理科学和化学", "天文学、地球科学", "生物科学", "医药、卫生", "农业科学", "工业技术",
				"交通运输", "哲学、宗教", "航空、航天", "环境科学、安全科学", "综合性图书", "社会科学总论", "政治、法律", "军事", "经济", "文化、科学、教育、体育", "语言、文字",
				"文学" };
		bookTypeComboBox = new JComboBox(way);
		bookTypeComboBox.setPreferredSize(new Dimension(170, 22));
		JButton confirmButton = new JButton("确定添加");
		confirmButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addActionPerformed(e);
			}
		});
		bookPanel.add(new JLabel("        isbn"));
		bookPanel.add(isbnTextField);
		bookPanel.add(new JLabel("        书名"));
		bookPanel.add(bookNameTextField);
		bookPanel.add(new JLabel("        作者"));
		bookPanel.add(authorTextField);
		bookPanel.add(new JLabel("出版日期"));
		bookPanel.add(dateTextField);
		bookPanel.add(new JLabel("图书类型"));
		bookPanel.add(bookTypeComboBox);
		bookPanel.add(new JLabel("图书状态"));
		bookPanel.add(bookStateComboBox);
		bookPanel.add(confirmButton);
		this.setSize(270, 250);
		return bookPanel;
	}

	protected void addActionPerformed(ActionEvent e) {
		if (this.isEmpty(isbnTextField.getText())) {
			JOptionPane.showMessageDialog(null, "isbn不能为空！");
			return;
		}
		if (!isbnTextField.getText().matches("[0-9]{0,3}") || isbnTextField.getText().length() != 3) {
			JOptionPane.showMessageDialog(null, "请输入正确的isbn！");
			return;
		}
		if (this.isEmpty(bookNameTextField.getText())) {
			JOptionPane.showMessageDialog(null, "书名不能为空！");
			return;
		}
		if (this.isEmpty(authorTextField.getText())) {
			JOptionPane.showMessageDialog(null, "作者不能为空！");
			return;
		}
		if (this.isEmpty(dateTextField.getText())) {
			JOptionPane.showMessageDialog(null, "出版日期不能为空！");
			return;
		}
		if (!dateTextField.getText().matches("[0-9]{0,4}") || dateTextField.getText().length() != 4) {
			JOptionPane.showMessageDialog(null, "请输入正确的出版年份！");
			return;
		}
//		String bookType = (String) bookTypeComboBox.getSelectedItem();
//		String bookState = (String) bookStateComboBox.getSelectedItem();
		LibraryService service = new LibraryService();
		BookTypeDaoImpl bookTypedao = new BookTypeDaoImpl();
		Book book1 = new Book(isbnTextField.getText(), bookNameTextField.getText(), authorTextField.getText(),
				dateTextField.getText(), bookTypedao.getIdx((String) bookTypeComboBox.getSelectedItem()), (String) bookStateComboBox.getSelectedItem());
		service.addBook(book1);
		this.dispose();
		JOptionPane.showMessageDialog(null, "添加成功！");
	}

	/**
	 * 判断是否为空
	 */
	private boolean isEmpty(String str) {
		if (str == null || "".equals(str.trim())) {
			return true;
		} else {
			return false;
		}
	}
}
